<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$aCamForm = array();
require("_inc/const.campiDato.php");

if (reqVar('azione') == 'salva') {
	$msgPag = confIns($aCamForm);
} 
reqDef('tipo_valore','label');
reqDef('segnala_variaz','0');
reqDef('situaz_progr','S');

$sCmdR = "";
$sCmdR .= "<a class='cmd1' href=\"javascript:document.frm1.azione.value='salva';document.frm1.submit();\" >salva</a>";
$sCmdR .=  cmdTorna('torna'); 

testaHtml('Gestione modelli',$msgPag,'modl');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','reload');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('sd');
echo getHtmlHidden('ttd');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="lab1" colspan='20' align='center'>Definizione Dato</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm);
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         <? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function confIns($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_dato'] = strtoupper(trim(reqVar('cod_dato')));
	if (reqVar('cod_dato')=='') {
		$msg .= _t("Codice obbligatorio")."<br>";
	}
	if (preg_match("/[^_A-Z0-9]/",reqVar('cod_dato'))) {
		$msg = _t("Codice '%s' puo' contenere solo lettere numeri e '_'",reqVar('cod_dato'))."<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	$sqlSel = "SELECT cod_dato FROM rgl_def_dato " .
			" WHERE cod_dato = '".reqVarAS('cod_dato')."' AND d.id_set_dati = ".reqVarInt('id_set_dati');
	$objDB->exec($sqlSel);
	$aRow = $objDB->fetch();
	if ($aRow) {
		$msg .= _t("Codice gia' esistente")."<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	//check script
	$script = reqVarTrim("script");
	$objParse = new rglParser;
	if(!$objParse->parse($script)) {
		return $objParse->getError();
	}
	$aVarAbil = getVarAbilCalcDato(reqVar('id'),reqVar('cod_dato')); 
	//dbg_("aVarAbil: ".print_r($aVarAbil,1));
	//dbg_("objParse->aVariables: ".print_r($objParse->aVariables,1));
	foreach($objParse->aVariables as $varName => $aPropVar) {
		if (strpos('TRS_',$varName[0])!== false) { // temporary or system variable
			continue;
		}
		if (!isset($aVarAbil[$varName])) {
			$msg .= _t("variabile '%s' non disponibile",$varName)."<br>";
			continue;
		}
		if ($aPropVar['modified'] and $aVarAbil[$varName] == 'non-modif') {
			$msg .= _t("variabile '%s' non modificabile",$varName)."<br>";
		}
	}

	$aFunzDisp = array();  // TODO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	foreach($objParse->aFunctions as $funzName => $aFunzVar) {
		if (!isset($aFunzDisp[$funzName])) {
			$msg .= _t("function '%s' not available",$funzName)."<br>";
			continue;
		}
		if ($aFunzVar['numPar'] !=  count($aFunzDisp[$funzName])) {
			$msg .= _t("function '%s' with %s parameters - required %s",
				$funzName,$aFunzVar['numPar'],count($aFunzDisp[$funzName]))."<br>";
		}
	}
	if($msg != "") {
		return $msg;
	}	
	if (reqVar('reformat')=='S') {
		$script = $objParse->getFormatCode();
	}
	$sSql = "INSERT INTO rgl_def_dato SET cod_dato = '".reqVarAS('cod_dato')."'"
		.",id_set_dati = ".reqVarInt('sd')
		.",tipo_tab_dati = ".reqVarInt('ttd')
		.",cod_sez = '".reqVarAS('cod_sez')."'"
		.",descr = '".reqVarAS('descr')."'"
		.",cod_dato_padre = '".reqVarAS('cod_dato_padre')."'"
		.",tipo_valore = '".reqVarAS('tipo_valore')."'"
		.",flag_quad_calc = '".reqVarAS('flag_quad_calc')."'"
		.",cod_fonte = '".reqVarAS('cod_fonte')."'"
		.",situaz_progr = '".reqVarAS('situaz_progr')."'"
		.",segnala_variaz = ".reqVarInt('segnala_variaz')
		.",script = '".addslashes($script)."'";
	$objDB->exec($sSql);
	$newId = $objDB->lastInsertId('insRec');
	
	header("Location: ".urlTorna("Dato '".reqVar('cod_dato')."' censito"));
	exit();
}
